package com.dfdchem.imd.kaiwu.webView;

import android.app.Activity;
import android.webkit.JavascriptInterface;

import com.dfdchem.imd.kaiwu.manager.DataManager;
import com.dfdchem.imd.kaiwu.model.Resp;
import com.dfdchem.imd.kaiwu.model.User;
import com.dfdchem.imd.kaiwu.util.JSON;
import com.dfdchem.imd.kaiwu.util.Log;
import com.dfdchem.imd.kaiwu.util.StringUtil;
import com.just.agentweb.AgentWeb;
import com.tencent.smtt.sdk.WebView;

public class WebViewInterface {
    private static final String TAG="WebViewInterface";

    private AgentWeb agent;
    private WebView mWebView;
    private Activity activity;
    private WebViewInterfaceCallback interfaceCallback;

    public WebViewInterface(AgentWeb agent, Activity activity, WebViewInterfaceCallback interfaceCallback) {
        this.agent = agent;
        this.activity = activity;
        this.interfaceCallback = interfaceCallback;
    }

    public WebViewInterface(WebView webView, Activity activity, WebViewInterfaceCallback interfaceCallback) {
        this.mWebView = webView;
        this.activity = activity;
        this.interfaceCallback = interfaceCallback;
    }

    @JavascriptInterface
    public void invoke_native(String method) {
        invoke_native(method, null, null);
    }

    @JavascriptInterface
    public void invoke_native(String method, String callback) {
        invoke_native(method, callback, null);
    }

    /**
     * var cb1 = function(resp){
     *     alert(JSON.stringify(resp))
     * }
     *
     * bindClick("#btn2",function(){
     *     前端调用方式 参数3这里必须用字符串才行
     *     window.KWJSBridge.invoke_native("getToken", null, "cb1");
     * });
     * @param method
     * @param params
     * @param callback
     */
    @JavascriptInterface
    public void invoke_native(String method, String callback, String params) {
        Log.e(TAG,"method=" + method + ";callback=" + callback + ";params=" +params);

        Resp<Object> resp = new Resp<>();
        if (!StringUtil.isEmpty(method)) {
            switch (method) {
                case "getToken":
                    resp.setCode(200);
                    resp.setData(DataManager.getInstance().getXToken());
                    break;
                case "getUserInfo":
                    User user = DataManager.getInstance().getUser();
                    resp.setCode(200);
                    resp.setData(user);
                    break;
                case "goLogin":
                    if(interfaceCallback != null){
                        interfaceCallback.goLogin(callback);
                    }
                    break;
            }
        }

        String respStr = JSON.toJSONString(resp);
        Log.d(TAG, "respStr:" + respStr);

        if(agent != null){
            agent.getJsAccessEntrace().quickCallJs(callback, respStr);
        }
        if (mWebView != null) {
            mWebView.post(new Runnable() {
                @Override
                public void run() {
                    mWebView.evaluateJavascript("javascript:"+callback+"(" + respStr + ")", null);;
                }
            });
        }
    }
}
